<?php
/**
 * Created by PhpStorm.
 * User: 86188
 * Date: 2021/3/6
 * Time: 10:22
 */

namespace app\index\controller;


use app\service\wxtplService;
use think\Db;

class Roomcheck extends Basewx
{
    public function index(){
        $id=request()->get("id");

        $info=Db::table("roomcheck")->where("id",$id)->find();
        if(empty($info)){
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>该请求信息不存在</div>";
        }
        if($info["check_status"]!="2"){
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>已审核完成</div>";
        }
        $this->assign("info",$info);

        $roominfo=Db::table("roominfo")->where("id",$info["roomid"])->find();
        if(empty($roominfo)){
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>该房屋信息不存在</div>";
        }

        $this->assign("roominfo",$roominfo);
        return View::fetch();
    }

    //保存
    public function checksuccess(){
        $id=request()->get("id");
        $info=Db::table("roomcheck")->where("id",$id)->find();
        if(empty($info)){
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>该请求信息已失效</div>";
        }
        if($info["check_status"]!="2"){
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>该房屋已审核完成</div>";
        }
        $this->assign("info",$info);

        $roominfo=Db::table("roominfo")->where("id",$info["roomid"])->find();
        if(empty($roominfo)){
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>该房屋不存在</div>";
        }

        Db::startTrans();
        try{
            //更新房屋信息
            Db::table('roominfo')
                ->where("id",$info["roomid"])
                ->update([
                        "owner_name"=>$info["owner_name"],
                        "owner_phone"=>$info["owner_phone"]
                    ]
                );

            Db::table("roomcheck")
                ->where("id",$id)
                ->update([
                    "check_status"=>1,
                    "check_time"=>time(),
                    "check_openid"=>session("user_id")
                ]);
            // 提交事务
            Db::commit();

            //给用户发送账单更新成功通知
            $admininfo=Db::table("admin")->where("name",$info["username"])->find();
            if((!empty($admininfo))&&($admininfo["openid"]!='0')){
                $openid=$admininfo["openid"];
                $url="";
                $first="您的房屋信息修改成功";
                $keyword1="房屋修改";

                $keyword2="修改成功";
                $keyword3="";
                $remark="";
                $data=(new wxtplService())->json_tempalte_checksuccess($openid,$url,$first,$keyword1,$keyword2,$keyword3,$remark);
                $access_token=(new Basewxget())->gettoken();
                $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
                $res=(new wxtplService())->curl_post($url,$data);

            }
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>房屋审核成功</div>";
        } catch (\Exception $e) {
            // 回滚事务

            Db::rollback();
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>".$e->getMessage()."</div>";
        }


    }

    public function checkfail(){
        $id=request()->get("id");
        $info=Db::table("roomcheck")->where("id",$id)->find();
        if(empty($info)){
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>该信息不存在</div>";
        }
        if($info["check_status"]!="2"){
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>该房屋已审核完成</div>";
        }
        $res=Db::table("roomcheck")
            ->where("id",$id)
            ->update([
                "check_status"=>3,
                "check_time"=>time(),
                "check_openid"=>session('user_id')
            ]);
        if($res){
            $admininfo=Db::table("admin")->where("name",$info["username"])->find();
            if((!empty($admininfo))&&($admininfo["openid"]!='0')){
                $openid=$admininfo["openid"];
                $url="";

                $first="您的房屋修改信息被驳回";
                $keyword1="审核退回";
                $roominfo=Db::table("roominfo")->where("id",$info["roomid"])->find();
                $communityinfo=Db::table("community")->where("community_id",$info["community_id"])->find();
                $keyword2=$communityinfo["community_name"]."-".$roominfo["address"];
                $remark="";
                $data=(new wxtplService())->json_tempalte_back($openid,$url,$first,$keyword1,$keyword2,$remark);
                $access_token=(new Basewxget())->gettoken();
                $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
                $res=(new wxtplService())->curl_post($url,$data);
                //Log::write($res,"wxusermsg");

            }
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>该房屋审核已被驳回</div>";
        }else{
            return "<div style='text-align: center;font-size:60px;margin-top:50px'>该房屋审核失败</div>";
        }
    }
}